$(function(){
	page.bind();
	queryRules();
})

var page = {
	bind:function(){
		$("#workOvertimeWayDiv input[name='calcType']").on("click",function(){
			if($(this).attr("data-durationType")!=1){
				$("#overtimeSettingDiv").show();
			}else{
				$("#overtimeSettingDiv").hide();
			}
		});
		//点击添加规则时，打开模态框，显示确认按钮
		$("#addRules").bind("click",function(){
			$("#add").show();
			$("#update").hide();
			$("#ruleName").val("");
			$("#addRulesModal").modal();
		});
		//开关按钮
		$("#switch").bind("click",function() {
			if ($(this).hasClass("switch-on")) {
				$("#workOvertimeWayDiv").hide();
			}else{
				$("#workOvertimeWayDiv").show();
			}
		});
		//新增确认按钮
		$("#add").bind("click",function(){
			var postData = page.postData();
			if(!postData){
				return;
			}
			ajaxUtil.ajaxRequest("dingTalk/saveAttendOvertimeRule",postData)
			.then(function(res){
				ajaxTable.ajax.reload(null,false);
				layer.msg(res,{icon:1});
				$("#addRulesModal").modal("hide");
			});
		});
		//修改规则按钮
		$("#update").bind("click",function(){
			var postData = page.postData();
			if(!postData){
				return;
			}
			postData.id = $(this).val();
			ajaxUtil.ajaxRequest("dingTalk/updateAttendOvertimeRule",postData)
			.then(function(res){
				ajaxTable.ajax.reload(null,false);
				layer.msg(res,{icon:1});
				$("#addRulesModal").modal("hide");
			});
		});
	},
	postData:function(){
		var ruleName = $("#ruleName").val();
		if(ruleName.length<=0){
			layer.msg("加班规则名称不能为空",{icon:2});
			return false;
		}
		if(ruleName.length>20){
			layer.msg("加班规则名称长度过长",{icon:2});
			return false;
		}
		var element = $("#workOvertimeWayDiv input[name='calcType']:checked");
		var calcType = element.attr("data-calcType");//是否需要审批 1：需要审批，2：不需要审批，3：不允许加班
		var durationType = element.attr("data-durationType");//加班时间计算 1：以审批单为准，2：以打卡时间为准
		var startTime = $("#startTime").val();
		startTime = startTime<0?0:startTime>720?720:startTime;
		var timeUnit = $("#timeUnit").val();
		timeUnit = timeUnit<1?1:timeUnit>720?720:timeUnit;
		if(!$("#switch").hasClass("switch-on")) {
			calcType = 3;
			durationType = 1;
			startTime = 30;
			timeUnit = 30;
		}
		var postData = {
				"ruleName":ruleName,
				"calcType":calcType,
				"durationType":durationType,
				"startTime":startTime,
				"timeUnit":timeUnit
		}
		return postData;
	}
}
//查询加班规则
var ajaxTable;
function queryRules(){
	$('#workOvertimeRulesTable tbody').unbind("click");
	ajaxTable = ajaxUtil.dataTablesAjaxWithcolumnDefs("workOvertimeRulesTable",10,"dingTalk/queryAttendOvertimeRule",
			{},[
				{"data":"ruleName"},
				{"data":"content"},
				{"data":"id",render:function(data){
								return "<button class='edit-btn btn btn-primary radius' type='button' value='"+data+"'>编辑</button>  <button class='delete-btn btn btn-danger radius' type='button' value='"+data+"'>删除</button>";
							}
				},
				{"data":"calcType"},
				{"data":"durationType"},
				{"data":"startTime"},
				{"data":"timeUnit"}
			],
			[
				{"targets": [3],"visible":false},
				{"targets": [4],"visible":false},
				{"targets": [5],"visible":false},
				{"targets": [6],"visible":false}
			]);
	layer.msg("查询完成",{icon:1});
	//编辑按钮点击事件
	$("#workOvertimeRulesTable tbody").on("click",".edit-btn",function(){
			var id = $(this).attr("value");
			$("#add").hide();
			$("#update").show();
			$("#update").val(id);
			//获取点击的行号
			var rowIndex = $(this).parents("tr").index();  
			var data = ajaxTable.row(rowIndex).data();
			//获取隐藏列的数据
			var ruleName = data.ruleName;
			var calcType = data.calcType;
			var durationType = data.durationType;
			var startTime = data.startTime;
			var timeUnit = data.timeUnit;
			//获取此行的数据
			$("#ruleName").val(ruleName);
			//判断选择加班计算方式
			if(calcType==3 || (calcType==1 && durationType==1)){
				$("#workOvertimeWayDiv input[name='calcType']:eq(0)").attr("checked",true).trigger("click");
			}else if(calcType==1 && durationType==2){
				$("#workOvertimeWayDiv input[name='calcType']:eq(1)").attr("checked",true).trigger("click");
			}else if(calcType==2 && durationType==2){
				$("#workOvertimeWayDiv input[name='calcType']:eq(2)").attr("checked",true).trigger("click");
			}
			//判断选择是否允许加班
			if(calcType===3 && $("#switch").hasClass("switch-on")){
				$("#switch").trigger("click");
			}else if(calcType!=3 && !$("#switch").hasClass("switch-on")){
				$("#switch").trigger("click");
			}
			$("#startTime").val(startTime);
			$("#timeUnit").val(timeUnit);
			$("#addRulesModal").modal("show");
	});
	//点击删除按钮时
	$("#workOvertimeRulesTable tbody").on("click",".delete-btn",function(){
		var con = confirm("是否删除该加班规则？");
		if(con==false){
			return;
		}
		var id = $(this).val();
		ajaxUtil.ajaxRequest("dingTalk/deleteAttendOvertimeRule",{id:id})
		.then(function(res){
			ajaxTable.ajax.reload(null,false);
			layer.msg(res,{icon:1});
		});
	});
}


